package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.common.utils.Common;
import com.think.teaching.mapper.ExamPaperMapper;
import com.think.teaching.model.CourseTag;
import com.think.teaching.model.ExamPaper;
import com.think.teaching.model.PaperTag;
import com.think.teaching.model.PaperZone;
import com.think.teaching.model.Question;
import com.think.teaching.model.Tag;
import com.think.teaching.service.ExamPaperService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试卷 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class ExamPaperServiceImpl extends BaseServiceImpl<ExamPaperMapper, ExamPaper> implements ExamPaperService {

	private final PaperZoneServiceImpl paperZoneServiceImpl;

	private final ExamTestServiceImpl examTestServiceImpl;

	private final PaperTagServiceImpl paePaperTagServiceImpl;

	public ExamPaperServiceImpl(PaperZoneServiceImpl paperZoneServiceImpl, ExamTestServiceImpl examTestServiceImpl,
			PaperTagServiceImpl paePaperTagServiceImpl) {
		this.paperZoneServiceImpl = paperZoneServiceImpl;
		this.examTestServiceImpl = examTestServiceImpl;
		this.paePaperTagServiceImpl = paePaperTagServiceImpl;
	}

	@Override
	@Transactional
	public ExamPaper AddExamPaper(ExamPaper examPaper) {
		if (this.save(examPaper)) {
			PaperZone zone1 = createPaperZone(1, examPaper.getId(), Question.Multiple_Choice);
			PaperZone zone2 = createPaperZone(2, examPaper.getId(), Question.True_Or_False);
			PaperZone zone3 = createPaperZone(3, examPaper.getId(), Question.Fill_The_Blank);
			PaperZone zone4 = createPaperZone(4, examPaper.getId(), Question.Short_Answer_Questions);
			paperZoneServiceImpl.saveBatch(Arrays.asList(zone1, zone2, zone3, zone4));
			List<PaperTag> tags = new ArrayList<>();
			for (int i = 0; i < examPaper.getTags().size(); i++) {
				PaperTag tag = new PaperTag();
				tag.setPaperId(examPaper.getId());
				tag.setTag(examPaper.getTags().get(i));

				tags.add(tag);
			}
			paePaperTagServiceImpl.saveBatch(tags);
			return examPaper;
		}
		return null;
	}

	private PaperZone createPaperZone(Integer index, Integer paperId, Integer type) {
		PaperZone zone1 = new PaperZone();
		zone1.setPaperId(paperId);
		zone1.setZoneIndex(index);
		zone1.setWeight(1);
		zone1.setType(type);
		zone1.setState(0);
		zone1.setCreatedBy(Common.GetCurrentAuthor().getUserId());
		return zone1;
	}

	@Override
	@Transactional
	public Boolean DeleteExamPaper(Integer id) throws Exception {
		UpdateWrapper<ExamPaper> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).set("delflag", 1);
		if (this.update(queryWrapper)) {
			if (this.examTestServiceImpl.DeleteRecordsByPaperId(id)) {
				return true;
			}
			throw new Exception();
		}
		return false;
	}

	@Override
	@Transactional
	public ExamPaper UpdateExamPaper(ExamPaper examPaper) {
		if (this.updateById(examPaper)) {
			List<PaperTag> tags = new ArrayList<>();
			if (examPaper.getTags() != null)
				for (int i = 0; i < examPaper.getTags().size(); i++) {
					PaperTag tag = new PaperTag();
					tag.setPaperId(examPaper.getId());
					tag.setTag(examPaper.getTags().get(i));

					tags.add(tag);
				}
			QueryWrapper<PaperTag> delete = new QueryWrapper<>();
			delete.eq("paper_id", examPaper.getId());
			paePaperTagServiceImpl.remove(delete);
			paePaperTagServiceImpl.saveBatch(tags);
			return examPaper;
		}
		return null;
	}

	@Override
	public ExamPaper GetExamPaper(Integer id) {
		QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		ExamPaper paper = getOne(queryWrapper);
		if (paper != null) {
			QueryWrapper<PaperZone> zoneWrapper = new QueryWrapper<>();
			zoneWrapper.eq("paper_id", paper.getId()).orderByAsc("zone_index");
			paper.setZones(paperZoneServiceImpl.list(zoneWrapper));
			paper.setTagInfos(paePaperTagServiceImpl.getBaseMapper().getPaperTagByPaperId(paper.getId()));
		}
		return paper;
	}

	public IPage<Map<String, Object>> ListExamPaperWithTagAndType(Integer count, Integer page, Integer type, Integer tag,
			Boolean withoutBan, Integer course) {

		return this.baseMapper.selectPaperWithFilter(new Page<Map<String, Object>>(page, count),
				type, tag, withoutBan ? 1 : 0,
				course);
	}

	public List<ExamPaper> SearchExamPaperWithKey(String key, Boolean withoutBan,Integer id) {
		QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("title", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		if (id != null && id>= 0) {
			queryWrapper.eq("course_id", id);
		}
		queryWrapper.last("LIMIT 0,10");
		return list(queryWrapper);
	}

	@Override
	public ExamPaper SetExamPaperState(Integer id, Integer state) {
		UpdateWrapper<ExamPaper> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}


	/**
	 * 删除和指定的courseId相关的试卷
	 * 
	 * @param courseId
	 * @return
	 */
	public Boolean DeleteRecordsByCourseId(Integer courseId) {
		UpdateWrapper<ExamPaper> queryWrapper = new UpdateWrapper<ExamPaper>();
		queryWrapper.eq("course_id", courseId).set("delflag", 1);
		return this.update(queryWrapper);
	}

}
